package com.five.controller;

import com.five.dto.RegisterDto;
import com.five.exception.BusinessException;
import com.five.result.Result;
import com.five.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    /**
     * 获取验证码图片
     * @param code
     * @return
     */
    @PostMapping("/verifyCode/image/{code}")
    public Result getVerifyCodeImage(@PathVariable String code){
        try {
            String base64Image = registerService.getBase64Image(code);
            return Result.success(base64Image);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 验证图片验证码
     * @param inputCode
     * @param key
     * @return
     */
    @PostMapping("/verifyCode/verifyImageVerifyCode")
    public Result VerifyImageVerifyCode(String inputCode, String key){
        try {
           registerService.verifyImageVerifyCode(inputCode,key);
            return Result.success("图形验证码通过");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 发送手机验证码
     * @return
     */
    @PostMapping("sendPhoneVerifyCode/{phoneNumber}")
    public Result sendPhoneVerifyCode(@PathVariable String phoneNumber){
        try {
            String key = registerService.sendPhoneVerifyCode(phoneNumber);
            return Result.success(key);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 验证手机验证码
     * @return
     */
    @PostMapping("/verifyCode/verifyPhoneVerifyCode")
    public Result verifyPhoneVerifyCode(String key, String inputCode){
        try {
            registerService.verifyPhoneVerifyCode(key,inputCode);
            return Result.success("手机验证码通过");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 手机号注册用户
     * @param registerDto
     * @return
     */
    @PostMapping("/registerPhone")
    public Result registerPhone(@RequestBody RegisterDto registerDto){
        try {
            registerService.registerPhone(registerDto);
            return Result.success("手机号注册成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }


    /**
     * 发送邮箱验证码
     * @return
     */
    @PostMapping("sendEmailVerifyCode/{email}")
    public Result sendEmailVerifyCode(@PathVariable String email){
        try {
            String key = registerService.sendEmailVerifyCode(email);
            return Result.success(key);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 邮箱注册用户
     * @param registerDto
     * @return
     */
    @PostMapping("/registerEmail")
    public Result registerEmail(@RequestBody RegisterDto registerDto){
        try {
            registerService.registerEmail(registerDto);
            return Result.success("邮箱注册成功");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }

    /**
     * 验证手机验证码
     * @return
     */
    @PostMapping("/verifyCode/verifyEmailVerifyCode")
    public Result verifyEmailVerifyCode(String key, String inputCode){
        try {
            registerService.verifyEmailVerifyCode(key,inputCode);
            return Result.success("手机验证码通过");
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return Result.error("系统异常");
        }
    }
}
